﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Program10
{
    class Program
    {
        static void Main(string[] args)
        {
            //var p = new Person
            //{
            //    No = "007",
            //    Name = "阿西吧",
            //    Age = 20
            //};
            #region 把对象存放到文件中
            //using (FileStream fsWrite=new FileStream("person.db", FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    //把对象内容转换成二进制，序列化
            //    var buffer = new BinaryFormatter();
            //    buffer.Serialize(fsWrite,p);
            //}
            //Console.WriteLine("序列化成功，存储成功");
            #endregion

            #region 从文件中读取对象的内容
            Person p;
            using(FileStream fsRead=new FileStream("person.db", FileMode.OpenOrCreate, FileAccess.Read))
            {
                var buffer = new BinaryFormatter();
                p = (Person)buffer.Deserialize(fsRead);
            }
            Console.WriteLine("工号：{0}，名字：{1}，年龄：{2}", p.No, p.Name, p.Age);
            #endregion

            Console.ReadLine();
        }
    }
    //定义类时要加标识，表示可以序列化
    [Serializable]
    public class Person
    {
        //工号
        public string No { get; set; }
        //名字
        public string Name { get; set; }
        //年龄
        public int Age { get; set; }
    }
}
